<?php
require 'Junction/Db/Creole/Adapter.php';
require 'Junction/Db/Creole/Service.php';

class Test_Junction_Db_Creole_Service extends PHPUnit_Framework_TestCase {
	
	private $_insertQuery = 'INSERT INTO users (user_id, email, name, password, date_added) VALUES (null, \'foo@bar\', \'foobar\', \'password\', 1189392979)';
	private $_insertPreparedQuery = 'INSERT INTO users (user_id, email, name, password, date_added) VALUES (?, ?, ?, ?, ?)';
	
	private $_updateQuery;
	private $_updatePreparedQuery = 'UPDATE users SET date_added = ? WHERE user_id = ?';
	
	private $_selectQuery = 'SELECT * FROM users';
	private $_selectPreparedQuery = 'SELECT * FROM users WHERE date_added < ?';
	
	private $_deleteQuery = 'DELETE FROM users WHERE email = \'foo@bar\'';
	
	public function __construct() {
		$this->_updateQuery = 'UPDATE users SET date_added = ' . time() . ' WHERE user_id = 1';
		$dsn = array(
			'db_driver' => 'mysql',
			'db_host' => 'localhost',
			'db_name' => 'JunctionTest',
			'db_user' => 'JunctionUser',
			'db_pass' => 'junction');
			
		$adapter = new Junction_Db_Creole_Adapter();
		$adapter->connectTo($dsn);
			
		$this->_dbh = new Junction_Db_Creole_Service();
		$this->_dbh->connect($adapter);
		$this->assertTrue(true);
	}
	
	/**
	 * Database service
	 *
	 * @var Junction_Db_Service
	 */
	private $_dbh;
	
	public function testInsert() {
		try {
			$this->assertTrue($this->_dbh->save($this->_insertQuery));
			$this->assertTrue($this->_dbh->lastInsertId() > 0);
		} catch (Junction_Db_Exception $e) {
			$this->fail();
		}
		
		try {
			$this->assertTrue($this->_dbh->save($this->_insertPreparedQuery, array(null, 'foo@bar', 'foobar', 'password', 1189392979)));
			$this->assertTrue($this->_dbh->lastInsertId() > 0);
		} catch (Junction_Db_Exception $e) {
			$this->fail();
		}
	}
	
	public function testUpdate() {
		try {
			$this->assertTrue($this->_dbh->save($this->_updateQuery));
			$this->assertTrue($this->_dbh->affectedRows() > 0);
		} catch (Junction_Db_Exception $e) {
			$this->fail();
		}
		
		try {
			$this->assertTrue($this->_dbh->save($this->_updatePreparedQuery, array(time() + 20, 1)));
			$this->assertTrue($this->_dbh->affectedRows() > 0);
		} catch (Junction_Db_Exception $e) {
			$this->fail();
		}
	}
	
	public function testSelect() {
		try {
			$results = $this->_dbh->select($this->_selectQuery);
			$rows = array();
			foreach ($results as $row) {
				$rows[] = $row;
			}
			$this->assertTrue(count($rows) > 0);
			
		} catch (Junction_Db_Exception $e) {
			$this->fail();
		}
		
		try {
			$results = $this->_dbh->select($this->_selectPreparedQuery, array(time()));
			$rows = array();
			foreach ($results as $row) {
				$rows[] = $row;
			}
			$this->assertTrue(count($rows) > 0);
		} catch (Junction_Db_Exception $e) {
			$this->fail();
		}
	}
	
	public function testDelete() {
	    try {
			$this->assertTrue($this->_dbh->save($this->_deleteQuery));
			$this->assertTrue($this->_dbh->affectedRows() > 0);
		} catch (Junction_Db_Exception $e) {
			$this->fail();
		}
	}
}
?>